package com.xwaf.platform.system.provider.serviceimpl.pay;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.service.service.CrudService;
import com.xwaf.platform.common.utils.DateHelper;
import com.xwaf.platform.system.api.entity.pay.Apply;
import com.xwaf.platform.system.api.entity.pay.ApplyConfig;
import com.xwaf.platform.system.api.entity.pay.Order;
import com.xwaf.platform.system.api.service.pay.OrderService;
import com.xwaf.platform.system.provider.dao.pay.ApplyConfigDao;
import com.xwaf.platform.system.provider.dao.pay.ApplyDao;
import com.xwaf.platform.system.provider.dao.pay.OrderDao;

/**
 * 
 * <p>
 * 订单
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019-5-20 11:44:21 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Transactional(readOnly = true)
@Service("orderService")
public class OrderServiceImpl extends CrudService<OrderDao, Order> implements OrderService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private ApplyDao applyDao;
	@Autowired
	private ApplyConfigDao applyConfigDao;

	@Override
	public Page<Order> selPageList(Page<Order> page, Order order) {
		List<Order> list = orderDao.selPageList(page, order);
		page.setRecords(list);
		return page;
	}

	@Override
	public String checkOrder(Map<String, String> params) {
		String orderNo = params.get("passback_params");// 回传参数
		Order o = new Order();
		o.setOrderNo(orderNo);
		Order order = orderDao.selectOne(o);
		if (order != null && (order.getStatus()).equals("1")) {// 未支付则支付
			Apply apply = applyDao.selectById(order.getApplyId());
			checkArgument(apply != null, "操作失败,支付应用不存在");
			Wrapper<ApplyConfig> wrapper = new EntityWrapper<ApplyConfig>();
			wrapper.where("apply_id ={0}", apply.getId());
			List<ApplyConfig> applyConfigs = applyConfigDao.selectList(wrapper);
			checkArgument(applyConfigs != null && applyConfigs.size() > 0, "操作失败,支付参数配置不完整");
			Map<String, String> maps = new HashMap<String, String>();
			for (ApplyConfig applyConfig : applyConfigs) {
				maps.put(applyConfig.getKey(), applyConfig.getValue());
			}
			if (maps.get("alipay_public_key") == null) {
				checkArgument(false, "操作失败,支付参数配置不完整");
			}
			return maps.get("alipay_public_key");
		}
		return null;
	}

	@Override
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void notifyOrder(Map<String, String> params) {
		String orderNo = params.get("passback_params");// 回传参数
		Order order = new Order();
		order.setOrderNo(orderNo);
		Order so = orderDao.selectOne(order);
		Order o = new Order();
		o.setId(so.getId());
		o.setPayTime(DateHelper.parseDate(params.get("gmt_payment")));
		o.setThreeOrderNo(params.get("trade_no"));
		o.setThreeOutOrderNo(params.get("out_trade_no"));
		o.setBuyerLogonId(params.get("buyer_logon_id"));
		o.setStatus("2");
		o.setUpdateTime(new Date());
		orderDao.updateById(o);
	}
}
